package com.sky.controller.user;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import nonapi.io.github.classgraph.json.Id;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Api("地址簿接口")
@Slf4j
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;


    /**
     * 获取地址簿信息
     */
    @ApiOperation("获取地址簿信息")
    @GetMapping("/list")
    public Result<List<AddressBook>> getAll(){
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AddressBook::getUserId,userId);
        List<AddressBook> list = addressBookService.list(lambdaQueryWrapper);
        return Result.success(list);
    }

    /**
     * 添加收货地址
     */
    @ApiOperation("添加收货地址")
    @PostMapping
    public Result<String> addAddress(@RequestBody AddressBook addressBook){
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);
        addressBookService.save(addressBook);
        return Result.success("新增地址成功");
    }

    /**
     * 根据ID查询收获地址的详细信息
     */
    @ApiOperation("根据Id查询收获地址的详细信息")
    @GetMapping("/{Id}")
    public Result<AddressBook> getById(@PathVariable Long Id){
        AddressBook addressBook = addressBookService.getById(Id);
        return Result.success(addressBook);
    }

    /**
     * 修改地址
     */
    @ApiOperation("修改地址")
    @PutMapping
    public Result<String> update(@RequestBody AddressBook addressBook){
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);
        addressBookService.updateById(addressBook);
        return Result.success("修改地址成功");
    }
    /**
     * 删除地址
     */
    @ApiOperation("删除地址")
    @DeleteMapping("/")
    public Result<String> deleteById(Long id){
        addressBookService.removeById(id);
        return Result.success("删除成功");
    }

    /**
     * 设置为默认地址
     */
    @ApiOperation("设置为默认地址")
    @PutMapping("/default")
    public Result<String> setDefault(@RequestBody AddressBook addressBookId){
        Long id = addressBookId.getId();
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AddressBook::getIsDefault,1);
        AddressBook defaultAddress = addressBookService.getOne(lambdaQueryWrapper);
        AddressBook addressBook = addressBookService.getById(id);
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        if(defaultAddress!=null){
            //已经存在默认地址
            defaultAddress.setIsDefault(0);
            addressBookService.updateById(defaultAddress);
        }
        return Result.success("默认地址设置成功");
    }

    /**
     * 查询默认地址
     */
    @ApiOperation("查询默认地址")
    @GetMapping("/default")
    public Result<AddressBook> getDefaultAddress(){
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AddressBook::getIsDefault,1);
        AddressBook addressBook = addressBookService.getOne(lambdaQueryWrapper);
        return Result.success(addressBook);
    }

}
